﻿using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using WMSADBUtility;

namespace ElectronicWMSA.List.Second
{
    public partial class UPS_Delete : Form
    {
        public UPS_Delete()
        {
            InitializeComponent();
            init();
            Type dgvType1 = DGV.GetType();
            PropertyInfo pi1 = dgvType1.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi1.SetValue(DGV, true, null);
        }

        #region 函数
        public void DBind()
        {
            string query = "select * from T_UPS_Excel_Manage order by Tracking_No asc";
            DataSet ds = DBHelper.Query(query);
            DGV.DataSource = ds.Tables[0];
        }

        private void UPS_Delete_Load(object sender, EventArgs e)
        {
            DBind();
        }

        #endregion

        #region 按钮：退出
        private void btnExit_Click(object sender, EventArgs e)
        {
            string query = "update T_UPS_Excel_Manage set Mark = 2 where Mark <> 1";
            DBHelper.Query(query);
            this.Close();
        }
        #endregion

        #region 按钮:删除
        private void bt_Delete_Data_Click(object sender, EventArgs e)
        {


            if (MessageClass.enquire("是否要进行批量删除的操作？" )== DialogResult.OK)
            {
                
                if (MessageClass.enquire("批量删除将会删除多行数据对表中数据造成重大影响，你是否依然要这么做？") == DialogResult.OK)
                {
                    string query = "delete from T_UPS_Excel_Manage where Mark = 7 ";
                    int result = DBHelper.ExecuteSql(query);
                    if (result > 0)
                    {
                        MessageClass.right("数据删除成功");
                        SystemVariable.AddLogInfo("批量删除", "操作员：【" + SystemVariable.Login_Name + "】进行了批量删除的操作");
                        DBind();
                    }

                    DBind();

                }

            }


        }


        #endregion

        #region 双击选中该行数据
        /// <summary>
        /// 双击选中该行数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DGV_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string Nid = "";
            string Mark = "";
            if (e.RowIndex == -1)
                return;

            if (DGV.RowCount > 0)
            {
                int rindex = e.RowIndex;
                Nid = DGV.Rows[rindex].Cells["nid"].Value.ToString();
                Mark = DGV.Rows[rindex].Cells["Mark"].Value.ToString();


                if(Mark == "7")
                {
                    DGV.Rows[rindex].DefaultCellStyle.BackColor = Color.White;
                    string query = "update T_UPS_Excel_Manage set Mark = 2 where nid = " + Nid;
                    DBHelper.Query(query);
                    DGV.Rows[rindex].Cells["Mark"].Value = 2;
                }
                else if (Mark == "1")
                {
                    MessageClass.error("该条数据中的订单号已经出库，不能删除");
                }
                else
                {
                    DGV.Rows[rindex].DefaultCellStyle.BackColor = Color.Red;
                    string query = "update T_UPS_Excel_Manage set Mark = 7 where nid = " + Nid;
                    DBHelper.Query(query);
                    DGV.Rows[rindex].Cells["Mark"].Value = 7;
                }

            }

            
        }
        #endregion

        #region 初始化
        /// <summary>
        /// 初始化
        /// </summary>
        public void init()
        {

            string query = "update T_UPS_Excel_Manage set Mark = 2 where Mark <> 1";
            DBHelper.Query(query);

        }
        #endregion

        private void Instruction_Click(object sender, EventArgs e)
        {
            Instruction_Delete ins = new Instruction_Delete();
            ins.ShowDialog();
        }
    }
}
